﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;//流

namespace Test1
{
    public partial class Form12 : Form
    {
        public Form12()
        {
            InitializeComponent();
        }
        //01
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                FileInfo info = new FileInfo(textBox1.Text);//FileInfo
                string strName = info.Name;
                string strFullName = info.FullName;
                string strCreateTime = info.CreationTime.ToLongDateString();
                long size = info.Length;
                string strReadonly = info.IsReadOnly.ToString();
                MessageBox.Show("文件名：" + strName 
                    + "\n文件完整路径：" + strFullName 
                    + "\n文件创建时间：" + strCreateTime 
                    + "\n文件大小：" + size 
                    + "B\n是否只读：" + strReadonly
                    );
            }
        }
        //02
        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath;
                DirectoryInfo info = new DirectoryInfo(textBox2.Text);
                FileSystemInfo[] fsinfos = info.GetFileSystemInfos(); //info.GetFiles获取所有文件 info.GetDirectoriesGet获取所有子文件夹
                foreach (FileSystemInfo f in fsinfos)
                {
                    if(f is DirectoryInfo)
                    {
                        DirectoryInfo dir = new DirectoryInfo(f.FullName);
                        listView1.Items.Add(dir.Name);
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(dir.Name);
                    }
                    else
                    {
                        FileInfo file = new FileInfo(f.FullName);
                        listView1.Items.Add(file.Name);
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(file.Name);
                    }
                }
            }
        }

        //03
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == string.Empty)
            {
                MessageBox.Show("要写入的内容不能为空！");
            }
            else
            {
                saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true);//mdzz
                    sw.WriteLine(textBox3.Text);
                    sw.Close();
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog2.Filter = "文本文件(*.txt)|*.txt";
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = "";
                StreamReader sr = new StreamReader(openFileDialog2.FileName);
                textBox3.Text = sr.ReadToEnd();
                sr.Close();
            }
        }
    }
}
